// pages/payment/payment.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ownerName:'',
    paymentList:[],
    item:'已缴费',
    names:'',
  },
  bindRefreshTap(e){
    const pages = getCurrentPages()
    const perpage = pages[pages.length - 1]
    perpage.onLoad()  
  },
  toFeesPending(e){
    console.log(e.currentTarget.dataset.id);
    wx.request({
      url: 'http://localhost/chargeDetail/getChargeId',
      data:{
        chargeItemId:e.currentTarget.dataset.id
      },
      success:function(result){
        console.log(result.data);
        wx.setStorageSync('chargeDetail', JSON.stringify(result.data))
      }
    })
      wx.navigateTo({
        url: '/pages/feesPending/feesPending',
      })
  },
  //搜索
  bindSearchConfirm(e){
    let that=this;
    console.log(e.detail.value);
    console.log(this.data.item);
    if(this.data.item=='已缴费'){
      this.setData({
        item:'1'
      })
    }else{
      this.setData({
        item:'0'
      })
    }
    console.log(this.data.item);
    wx.request({
      url: 'http://localhost/chargetItem/search',
      data:{
        contractor:this.data.ownerName,
        name:e.detail.value,
        status:this.data.item,
    
      },
      success:function(result){
         if(that.data.item=='0'){
          that.setData({
            item:'待缴费'
          })
        }else{
          that.setData({
            item:'已缴费'
          })
        }
        console.log(that.data.item);
        console.log(result.data);
        that.setData({
          paymentList:result.data
        })
       
      }
    })
    
  },
  onStatus(e){
    
    this.setData({
      item:'待缴费'
    })
    if(this.data.item=='已缴费'){
      this.setData({
        item:'1'
      })
    }else{
      this.setData({
        item:'0'
      })
    }
    console.log(e);
    let that=this;
    wx.request({
      url: 'http://localhost/chargetItem/getCharge',
      data:{
        contractor:that.data.ownerName,
        status:this.data.item,
        name:this.data.names
      },
      success:function(result){
        if(that.data.item=='0'){
          that.setData({
            item:'待缴费'
          })
        }else{
          that.setData({
            item:'已缴费'
          })
        }
        console.log(result.data);
        that.setData({
          paymentList:result.data
        })
      }
    })
  },
  onStatus2(){
  
    this.setData({
      item:'已缴费'
    })
    if(this.data.item=='已缴费'){
      this.setData({
        item:'1'
      })
    }else{
      this.setData({
        item:'0'
      })
    }
    let that=this;
    wx.request({
          url: 'http://localhost/chargetItem/getCharge',
          data:{
            contractor:that.data.ownerName,
            status:this.data.item,
            name:this.data.names
          },
          success:function(result){
            if(that.data.item=='0'){
              that.setData({
                item:'待缴费'
              })
            }else{
              that.setData({
                item:'已缴费'
              })
            }
            console.log(result.data);
            that.setData({
              paymentList:result.data
            })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    if(this.data.item=='已缴费'){
      this.setData({
        item:'1'
      })
    }else{
      this.setData({
        item:'0'
      })
    }
    let names=wx.getStorageSync('names');
    this.setData({
      names:names
    })

    let that=this;
     //读取用户的基本信息
     let userInfo=wx.getStorageSync('userInfo');

     if(userInfo){
       userInfo =JSON.parse(userInfo);
      that.setData({
        ownerName:userInfo.ownerName,
       })
       console.log(that.data.ownerName);
     }

    wx.request({
      url: 'http://localhost/chargetItem/getCharge',
      data:{
        contractor:that.data.ownerName,
        status:this.data.item,
        name:this.data.names
      },
      success:function(result){
        if(that.data.item=='0'){
          that.setData({
            item:'待缴费'
          })
        }else{
          that.setData({
            item:'已缴费'
          })
        }
        console.log(result.data);
        that.setData({
          paymentList:result.data
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})